import 'package:get/get.dart';
import 'package:getx_tutorial/app/data/models/news_model.dart';
import 'package:getx_tutorial/app/modules/home/news_repository.dart';

class NewsController extends GetxController with StateMixin<NewsItem> {
  final INewsRepository repository;
  var uniquekey = ''.obs;

  NewsController({required this.repository});

  @override
  void onInit() {
    super.onInit();

    // getNewsDetail(); // 初次调用
    // 监听一次数据的变化
    once(uniquekey, (value) => getNewsDetail());
  }

  // 获取新闻列表
  Future<void> getNewsDetail() async {
    print('controller: '+uniquekey.value);
    final res = await repository.getNewsDetail(uniquekey.value);

    print('=====Detail=============-----------');
    print(res.toJson());
    
    change(res, status: RxStatus.success());
  }
}